/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.UniversidadForm;
import com.uca.sisbec.helpers.manto.UniversidadHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Susy
 */
public class UniversidadAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UniversidadForm uf = (UniversidadForm) form;
        uf.setEstado(true);
        UniversidadHelper uh = new UniversidadHelper(getSession(request));
        request.setAttribute("UniversidadesList", uh.getAllUniversidades());
        return mapping.findForward("success");
    }

    public ActionForward GuardarUniversidad(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        UniversidadForm uf = (UniversidadForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        UniversidadHelper uh = new UniversidadHelper(getSession(request));

        if (uh.ExisteCodUniversidad(uf)) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.existe"));
            request.setAttribute("errors", errors);
        } else {
            uh.InsertarUniversidadDB(uf);
            getSession(request).flush();
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.nueva"));
            request.setAttribute("msg", msg);
        }
        request.setAttribute("UniversidadesList", uh.getAllUniversidades());
        return mapping.findForward("success");
    }

    public ActionForward InhabilitarUniversidad(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        UniversidadHelper uh = new UniversidadHelper(getSession(request));
        uh.InhabilitarUniversidadDB(Integer.parseInt(request.getParameter("cod")));
        getSession(request).flush();
        request.setAttribute("UniversidadesList", uh.getAllUniversidades());
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.desactivada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
    public ActionForward HabilitarUniversidad(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        UniversidadHelper uh = new UniversidadHelper(getSession(request));
        uh.HabilitarUniversidadDB(Integer.parseInt(request.getParameter("cod")));
        getSession(request).flush();
        request.setAttribute("UniversidadesList", uh.getAllUniversidades());
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("universidad.activada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
}
